package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程前端控制器
 * @author daygods
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    /**
     * 添加课程基本信息
     */
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody EduCourse eduCourse) {
        String id = courseService.saveCourseInfo(eduCourse);
        return R.ok().data("courseId", id);
    }

    /**
     * 课程分页查询
     */
    @GetMapping("/getCourseList/{current}/{limit}")
    public R getCourseList(@PathVariable long current, @PathVariable long limit) {
        Page<EduCourse> pageParam = courseService.getCoursePage(current, limit);
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
     * 根据课程id查询课程基本信息
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        EduCourse eduCourse = courseService.getById(courseId);
        return R.ok().data("courseInfo", eduCourse);
    }

    /**
     * 修改课程信息
     */
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody EduCourse eduCourse) {
        courseService.updateById(eduCourse);
        return R.ok();
    }

    /**
     * 根据课程id删除课程
     */
    @DeleteMapping("/deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        boolean flag = courseService.removeCourse(courseId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}